import { UserLoginApi, getIndexListApi, getCategoryLeftApi, getCategoryRightApi, postCategoryListApi, postBrandListApi, postDetailApi } from "@/api/index";

import QRcode from "qrcode";
const actions = {
    async actchangeIsLogin(context, parmas) {
        //进行请求
        let result = await UserLoginApi(parmas.user_name, parmas.user_password);
        // console.log(result);
        if (result.status == 200) {
            // localStorage.setItem("token", result.token);
            localStorage.setItem("token", JSON.stringify(result.token));
        }
        context.commit("changeIsLogin", result.token)
    },
    async actChangeIndexListDatas(context, parmas) {
        context.commit("changeIsLoading", true);
        let result = await getIndexListApi(parmas);
        if (result.status == "success") {
            context.commit("changeIndexListDatas", result.data);
        }
    },
    // 分类左侧
    async actChangeCategoryLeftDatas(context) {
        let result = await getCategoryLeftApi()
            // console.log(result);
        context.commit("changeCategoryLeftDatas", result.data)
    },
    // 分类右侧
    async actChangeCategoryRightDatas(context, cat_id) {
        context.commit("changeIsLoading", true)
        let result = await getCategoryRightApi(cat_id)
            // console.log(result);
        if (result.status == "success") {
            context.commit("changeIsLoading", false)
        }
        context.commit("changeCategoryRightDatas", result.data)
    },
    //分类列表
    async actChangeCategoryListDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await postCategoryListApi(params)
            // console.log(result);
        if (result.data.length != 0) {
            context.commit("changeCategoryListDatas", result.data)
            context.commit("changeIsLoading", false)
        } else {
            context.commit("changeIsLoading", false)

        }

    },
    // 品牌列表
    async actChangeBrandListDatas(context, params) {
        let result = await postBrandListApi(params)
            // console.log(result);
        context.commit("changeBrandListDatas", result.data)
    },
    // 详情页
    async actChangeDetailDatas(context, params) {
        let result = await postDetailApi(params)
            // console.log(result);
        let goods_thumb = result.data.goods_thumb
        let goods_name = result.data.goods_name
        let shop_price = result.data.shop_price

        function base64Fn(img) {
            //img.src 是一个字符串 xxxxx.png  // xxxxx.jpg
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            canvas.width = img.width //设置画布的宽度
            canvas.height = img.height //设置画布的高度
            img.crossOrigin = "anonymous" //设置图片跨域
            var ext = img.src.slice(img.src.lastIndexOf(".") + 1).toLowerCase() //jpg  png 
            ctx.drawImage(img, 0, 0, img.width, img.height)
            var dataUrl = canvas.toDataURL("image/" + ext)
            return dataUrl
        }
        var image = new Image() //<img/>
        image.src = goods_thumb

        var image2 = new Image() //<img/>
        image2.src = "http://192.168.0.182:8080/image/qm.png"
            //获取地址栏中的url
        var pageUrl = location.href
        var codeImg = await QRcode.toDataURL(pageUrl)

        // console.log(codeImg);
        var base64Img2 = ""
        image2.onload = function() {
            base64Img2 = base64Fn(image2)
        }
        image.onload = function() {
            var base64 = base64Fn(image)
                // console.log(base64Img2);
            var posterDatas = {
                posterImg: base64,
                goods_name: goods_name,
                shop_price: shop_price,
                codeImg: codeImg,
                qm: base64Img2
            }
            context.commit("changePosterImg", posterDatas)
        }



        // base64Fn(goods_thumb)
        context.commit("changeDetailDatas", result.data)

    }

};


export default actions;